
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
//import com.taobao.top.request.TradesSoldGetRequest;

public class taobao {

	public Cookie[] login(String name, String pas) {
		try {
			HttpClient client = new HttpClient();

			PostMethod post = new PostMethod("http://gw.api.taobao.com/router/rest");
			NameValuePair[] params = new NameValuePair[12];
			
			String responseString = getJdOrderInfoString(client, post, params,
					0);
			
			System.out.println(responseString);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private String getJdOrderInfoString(HttpClient client, PostMethod post,
			NameValuePair[] params, int page) throws IOException,
			HttpException, UnsupportedEncodingException {
		
		int count=0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long cur=System.currentTimeMillis();
		//Ĭ�ϲ�ѯһ���
		long start=cur-86400000;
		
		String app_key="12468635";
		String end_date=sdf.format(new Date(cur));
		end_date="";
		String timestamp=end_date;
		
		timestamp=sdf.format(new Date());
		String v="2.0";
		String method="taobao.trades.sold.get";
		String partner_id ="top-apitools";
		String session    ="6102b1126f95a0e500e10093a051a211595db9e0a708eed386766418";
		String format  ="xml";
		String page_size  ="100";
		String status     ="NEW_CREATED";		
		String fields="seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee,adjust_fee,post_fee,total_fee,pay_time,end_time,modified,consign_time,buyer_obtain_point_fee,point_fee,real_point_fee,received_payment,commission_fee,pic_path,num_iid,num_iid,num,price,cod_fee,cod_status,shipping_type,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders.title,orders.pic_path,orders.price,orders.num,orders.iid,orders.num_iid,orders.sku_id,orders.refund_status,orders.status,orders.oid,orders.total_fee,orders.payment,orders.discount_fee,orders.adjust_fee,orders.sku_properties_name,orders.item_meal_name,orders.buyer_rate,orders.seller_rate,orders.outer_iid,orders.outer_sku_id,orders.refund_id,orders.seller_type";
		
		
		
		
		TreeMap<String, String> apiparamsMap = new TreeMap<String, String>();
		// ��װЭ�����
		apiparamsMap.put("app_key", app_key);
		apiparamsMap.put("end_date", end_date);
		apiparamsMap.put("timestamp", timestamp);
		apiparamsMap.put("v", v);
		apiparamsMap.put("method", method);
		apiparamsMap.put("partner_id", partner_id);
		apiparamsMap.put("session", session);
		apiparamsMap.put("format", format);
		apiparamsMap.put("page_size", page_size);
		apiparamsMap.put("status", status);
		apiparamsMap.put("fields", fields);
		//apiparamsMap.put("app_key", app_key);
		
	
		
		// ���ǩ��ڶ�������Ϊ��������APP_SECRET
		String sign = Util.sign(apiparamsMap,"519b8bbccc941314ca90924d89eef6bc");
		// ��װЭ�����sign
		apiparamsMap.put("sign", sign);

		StringBuilder param = new StringBuilder();
		for (Iterator<Map.Entry<String, String>> it = apiparamsMap.entrySet()
				.iterator(); it.hasNext();) {
			Map.Entry<String, String> e = it.next();
//			param.append("&").append(e.getKey()).append("=").append(
//					e.getValue());
			
			params[count++]  = new NameValuePair(e.getKey(), e.getValue());	
		}
		
		//String 
		//System.out.println(((Date)sdf.parseObject("2012-12-21 00:00:00")).getTime()-((Date)sdf.parseObject("2012-12-20 00:00:00")).getTime());

		  
		  
 //  String sign = org.apache.commons.codec.digest.DigestUtils.md5Hex("");
   
   
 // System.out.println("����========"+org.apache.commons.codec.digest.DigestUtils.md5Hex("16A774A73558650EC4E2B0C5E39C9F3D360buy_param_json{\"return_id\":\"20032\",\"trade_no\":\"20032\"}method360buy.after.state.updatetimestamp2011-07-20 11:10:04v1.0vender_id2003216A774A73558650EC4E2B0C5E39C9F3D"));
   
  // System.out.println(key+"360buy_param_json"+_360buy_param_json+"method"+method+"timestamp"+timestamp+"v"+v+"vender_id"+vender_id+""+key);
		
  //    System.out.println(sign.toUpperCase());
//			params[count++]  = new NameValuePair("loginname", "jd_alife");
//			params[count++]  = new NameValuePair("loginpwd", "075526563237");
		
//		params[count++]  = new NameValuePair("timestamp", timestamp);		
//		params[count++]  = new NameValuePair("v", v);	
//		params[count++]  = new NameValuePair("app_key", app_key);
//		params[count++]  = new NameValuePair("method", method);		
//		params[count++]  = new NameValuePair("partner_id", partner_id);
//		
//		params[count++]  = new NameValuePair("session", session);
//		params[count++]  = new NameValuePair("format", format);		
//		params[count++]  = new NameValuePair("page_size", page_size);
//		params[count++]  = new NameValuePair("status", status);		
//		params[count++]  = new NameValuePair("page_no", page+"");
//		
//		params[count++]  = new NameValuePair("fields", fields);		
//		params[count++]  = new NameValuePair("sign", sign.toUpperCase());
//		
		
		//value="http://myrainbow.cn/?" name="returnUrl"/>
		
		post.setRequestBody(params);
		
		client.executeMethod(post);
		String			responseString = new String(post.getResponseBody(), "utf-8");
		return responseString;
	}
	
	

	public static void main(String[] args) {
		new taobao().login("zzzxxxzzz", "zzzxxxzzz");
		
		
		String ori="7ECDD597E163344C4C9EC43320822273360buy_param_json{\"end_date\":\"2011-12-23 13:36:57\",\"optional_fields\":\"\",\"order_state\":\"WAIT_SELLER_STOCK_OUT\",\"page\":\"1\",\"page_size\":null,\"start_date\":\"\"}method360buy.order.searchtimestamp2011-12-23 13:41:10v1.0vender_id117147ECDD597E163344C4C9EC43320822273";
		
		//System.out.println(org.apache.commons.codec.digest.DigestUtils.md5Hex(ori).toUpperCase());
	}

	
}